package com.sf.fy.uav.applet.order.controller.task;

import com.alibaba.fastjson.JSONObject;
import com.sf.fy.uav.applet.user.service.AboutUserService;
import com.sf.fy.uav.common.request.common.IdRequest;
import com.sf.fy.uav.common.request.order.task.SubmitTaskRequest;
import com.sf.fy.uav.common.request.order.task.TaskGoodsRequest;
import com.sf.fy.uav.common.request.order.task.TaskRequest;
import com.sf.fy.uav.common.response.IdResponse;
import com.sf.fy.uav.common.response.order.task.GoodsSoundRecordingResponse;
import com.sf.fy.uav.common.response.order.task.GoodsTypeResponse;
import com.sf.fy.uav.common.response.order.task.TaskStatusLogResponse;
import com.sf.fy.uav.common.response.user.TaskDetailResponse;
import com.sf.fy.uav.common.response.user.TaskResponse;
import com.sf.fy.uav.common.utils.Rs;
import com.sf.fy.uav.good.service.IGoodsSoundRecordingService;
import com.sf.fy.uav.task.rest.TaskRest;
import jakarta.validation.Valid;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.nio.file.Files;
import java.util.List;

/**
 * @author LTQ
 */
@RestController
@RequestMapping("/sys/order/task/")
public class TaskController {
    private static final Logger logger = LoggerFactory.getLogger(TaskController.class);
    @Autowired
    private TaskRest taskRest;

    @Autowired
    private AboutUserService aboutUserService;

    @Autowired
    private ResourceLoader resourceLoader;

    @Autowired
    private IGoodsSoundRecordingService iGoodsSoundRecordingService;

    @GetMapping("get-ing-tasks")
    public Rs<List<TaskResponse>> getIngTasks(@RequestParam String statuses) {
        return Rs.success(taskRest.getIngTasks(statuses));
    }

    @GetMapping("get-all-tasks")
    public Rs<List<TaskResponse>> getAllTasks() {
        return Rs.success(taskRest.getAllTasks());
    }

    @PostMapping("add-task")
    public Rs<IdResponse> addTask(@RequestBody @Validated TaskRequest taskRequest) {
        return Rs.success(aboutUserService.addTask(taskRequest));
    }

    @PostMapping("submit-task")
    public Rs<Void> submitTask(@Valid @RequestBody SubmitTaskRequest submitTaskRequest) {
        aboutUserService.submitTask(submitTaskRequest);
        return Rs.success();
    }

    @PostMapping("cancel-task")
    public Rs<Void> cancelTask(@RequestBody IdRequest idRequest) {
        aboutUserService.cancelTask(idRequest.getId());
        return Rs.success();
    }

    @GetMapping("get-order-info")
    public Rs<JSONObject> getOrderInfo(@RequestParam String orderCode) {
        return Rs.success(aboutUserService.getOrderInfo(orderCode));
    }

    @GetMapping("get-task-detail")
    public Rs<TaskDetailResponse> getTaskDetail(@RequestParam String id) {
        return Rs.success(aboutUserService.getTaskDetail(id));
    }

    @GetMapping("get-task-status-log")
    public Rs<List<TaskStatusLogResponse>> getTaskStatusLog(@RequestParam String taskId) {
        return Rs.success(aboutUserService.getTaskStatusLog(taskId));
    }


    @GetMapping("get-goods-sound-recording")
    public Rs<List<GoodsSoundRecordingResponse>> getGoodsSoundRecording(@RequestParam String taskId) {
        return Rs.success(aboutUserService.getGoodsSoundRecording(taskId));
    }

    @PostMapping("update-goods")
    public Rs<GoodsTypeResponse> updateGoods(@RequestBody TaskGoodsRequest taskGoodsRequest) {
        logger.info("修改物资信息(包含声音)入参{}",taskGoodsRequest);
        logger.info("修改物资信息(包含声音)入参{}",taskGoodsRequest.getRecordArr());
//        iGoodsSoundRecordingService.updateByTaskId(taskGoodsRequest);
        aboutUserService.updateGoods(taskGoodsRequest);
        return Rs.success();
    }

    @GetMapping("/audioDownload")
    public ResponseEntity<ByteArrayResource> down3() throws Exception {

        byte[] bytes = Files.readAllBytes(new File("D:\\goods\\record\\M800003m2sQw02KtPM.mp3").toPath());

        ByteArrayResource bar = new ByteArrayResource(bytes);

        return ResponseEntity.ok()

                .contentType(MediaType.APPLICATION_OCTET_STREAM)

                .header("Content-disposition", "attachment; filename=M800003m2sQw02KtPM.mp3")

                .body(bar);
    }
}
